// pages/minePage/toOrderDetails/toOrderDetails.js
import {
  lgRequest
} from "../../../utils/ajax";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    photerIcon: "https://www.mengbaoonline.com:8182/profile/upload/2022/11/24/ca4a458299232ae12fd7f07cae307ba5.png",
    shangChuang:'http://114.115.190.196:8082/profile/upload/2022/09/13/53ead76e72a504af6130b526634bf9a2.png',
    delectImgTag: "https://www.mingchuliangzao.com.cn:8632/profile/upload/2022/07/19/713a49f43240fae4d44090d92d7f9cf6.png",
    workList: "",
    orderList: "",
    problemType: "",
    fileList: [],
    showFailureBypass: false,
    params: {},
    remark: '',
    showOrderFlow: false,
    overPeople: "",
    orderType: [{
        problemType: "集成",
        typeCode: 1
      },
      {
        problemType: "设备",
        typeCode: 2
      },
      {
        problemType: "网络",
        typeCode: 3
      },
      {
        problemType: "电力",
        typeCode: 4
      },
    ],
    isType: 0,
    problemType: [],
  },
  getZhanInfo(){
    let params = {
      zId : this.data.undoneInfoList.zid

    }
    lgRequest('siteList',params,{
      doSuccess:r=>{
        this.setData({
          zhanInfo: r.data[0]
        })
      }
    })
  },
  // 关闭弹窗
  onClose() {
    let noProcessing = this.data.noProcessing;
    noProcessing.forEach(r => {
      r.is = false
    })
    let problemTag = this.data.problemTag;
    problemTag.forEach(r => {
      r.is = false
    })
    let params = {
      problemType: ''
    }
    this.setData({
      showFailureBypass: false,
      noProcessing,
      params,
      remark: "",
      showOrderFlow: false,
      showOrderCompletion: false,
      selectPeople: '',
      problemTag,
      problemType: '',

      showPic: false

    })
  },
  // 跳转到工单详情页
  toStateDetail(){

    let zhanInfo = encodeURIComponent(JSON.stringify(this.data.zhanInfo));
    wx.navigateTo({
      url: `../stateDetail/stateDetail?zhanIdInfo=${zhanInfo}`,
    })
  },
  // 展示暂不处理
  showFailureBypass(e) {
    console.log(e)
    this.data.params.wid = e.currentTarget.dataset.item.wid;
    this.setData({
      showFailureBypass: true
    })

  },
  // 上传相册
  smbitPic(e) {
    let wid = e.currentTarget.dataset.item.wid;
    wx.chooseMedia({
      count: 9,
      mediaType: ['image'],
      sourceType: ['album'],
      success:(res)=> {
        console.log(res.tempFiles[0].tempFilePath)
        console.log(res.tempFiles[0].size)
        console.log('图片图片图片', res)
        res.tempFiles.forEach(item => {
          let file = item.tempFilePath

          wx.uploadFile({
            url:"https://www.mengbaoonline.com:8182/common/upload",
            // url: "http://114.115.190.196:8082/common/upload",
            filePath: file,
            name: "file",
            formData: {
              user: "test"
            },
            header: {
              Authorization: getApp().globalData.Authorization
            },
            success: (res) => {
              // 上传完成需要更新 fileList 
              let data = JSON.parse(res.data);
              console.log(data)
              wx.showToast({
                title: "上传成功！",
              });
              // if (res.code == 200) {
                let params = {
                  wid,
                  over: data.url,
                }
                lgRequest("smbitQuestionPic", params, {
                  doSuccess: (res) => {
                    debugger
                    console.log('图片上传成功', res)
                    this.getOrderList()
                  }
                })
              // }

            },
            fail(error) {
              wx.showToast({
                title: "上传失败，请稍后再试！",
              });
              console.log(error)
            },
          });
          // let params = {
          //   wid,
          //   over:item.tempFilePath
          // }
          // lgRequest("smbitQuestionPic",params,{
          //   doSuccess:(res)=>{
          //     console.log('图片上传成功',res)
          //     this.getOrderList()
          //   }
          // })
        })

      }
    })

  },
  // 拍照
  phonePic(e) {
    let wid = e.currentTarget.dataset.item.wid;
    wx.chooseMedia({
      count: 9,
      mediaType: ['image'],
      sourceType: ['camera'],
      success: (res) => {
        console.log(res.tempFiles[0].tempFilePath)
        console.log(res.tempFiles[0].size)
        wx.uploadFile({
          url:"https://www.mengbaoonline.com:8182/common/upload",
          // url: "http://114.115.190.196:8082/common/upload",
          filePath: res.tempFiles[0].tempFilePath,
          name: "file",
          formData: {
            user: "test"
          },
          header: {
            Authorization: getApp().globalData.Authorization
          },
          success: (res) => {
            // 上传完成需要更新 fileList 
            let data = JSON.parse(res.data);
            console.log(data)
            wx.showToast({
              title: "上传成功！",
            });
            // if (res.code == 200) {
              let params = {
                wid,
                over: data.url,
              }
              lgRequest("smbitQuestionPic", params, {
                doSuccess: (res) => {
                  debugger
                  console.log('图片上传成功', res)
                  this.getOrderList()
                }
              })
            // }

          },
          fail(error) {
            wx.showToast({
              title: "上传失败，请稍后再试！",
            });
            console.log(error)
          },
        });


      }
    })

  },
  // 获取工单详情
  getWorkList() {
    let params = {
      zId: this.data.undoneInfoList.zid
    }
    lgRequest("getWorkList", params, {
      doSuccess: (r) => {
        console.log(r)
        this.setData({
          workList: r.data
        })
      }
    })
  },
  // 获取问题列表
  getOrderList() {
    let params = {
      zId: this.data.undoneInfoList.zid
    }
    lgRequest("dList", params, {
      doSuccess: (r) => {
        console.log(r)
        let orderList = r.data;

        // if(orderList.over){
        orderList.forEach(item => {
          item.content = item.content.replace("null", '')
          // let arr = [];
          item.pic = (item.over || '').split(',')
          // item.over = arr
          console.log('item.pic', item.pic)
        })
        // }

        this.setData({
          orderList: orderList
        })
      }
    })
  },
  //获取问题类型
  getProblem() {
    let serve = getApp().globalData.server;
    let Authorization = getApp().globalData.Authorization
    wx.request({
      url: `${serve}server/dictionaries/problem_type`,
      header: {
        Authorization
      },
      success: (r) => {
        console.log(r)
        this.setData({
          problemType: r.data.data
        })
      }
    })
  },
  // 获取暂不处理原因
  noProcessing() {
    let params = {
      code: "failure_bypass"
    }
    lgRequest("dictionaries", params, {
      doSuccess: r => {
        console.log(r)
        this.setData({
          noProcessing: r.data
        })
      }
    })
  },
  // 暂不处理原因
  selectFailureBypass(e) {
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    let params = this.data.params;
    params.failureBypass = item.valueId;
    params.state = 4;
    this.setData({
      params
    })


    console.log(e)
    let noProcessing = this.data.noProcessing;
    noProcessing[index].is = !noProcessing[index].is
    this.setData({
      noProcessing
    })
  },
  // 暂不处理说明
  getFailureBypassRemark(e) {
    console.log(e.detail.value)
    this.data.params.remark = e.detail.value
    console.log(this.data.params.remark)
  },
  submitFailureBypass() {
    let params = this.data.params
    lgRequest("smbitQuestionPic", params, {
      doSuccess: r => {
        console.log(r)
        this.getOrderList(); //获取工单列表
        this.onClose();

      }
    })
  },
    //上传图片
    afterRead(event) {
      let that = this;
      console.log(event)
      const {
        file
      } = event.detail;
      // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
      let url = getApp().globalData.obLineSever + "server/picState";
      file.forEach((itemFile) => {
        
          wx.uploadFile({
            // url: "http://114.115.190.196:8082/common/upload",
            // url:"https://www.mingchuliangzao.com.cn:8632/common/upload",
            url:"https://www.mengbaoonline.com:8182/common/upload",
            filePath: itemFile.path,
            name: "file",
            formData: {
              user: "test"
            },
            header: {
              "Content-Type": "multipart/form-data",
              Authorization: getApp().globalData.Authorization,
            },
            success: (res) => {
              // 上传完成需要更新 fileList 
              let data = JSON.parse(res.data);
              console.log(res)
              wx.showToast({
                title: "上传成功！",
              });
              let params = {
                siteId: that.data.zhanIdInfo.id,
                state: that.data.selectIndexType + 1,
                pic: data.url,
              }
              lgRequest("addPicState", params, {
                doSuccess: (res) => {
                  // console.log(res)
                  if (res.code == 200) {
                    console.log(res,);
                    this.picStateList()
                  }
                }
        
              })
             
  
            },
            fail(error) {
              wx.showToast({
                title: "上传失败",
              });
              console.log(error,'这是上传图片的错误')
            },
          });
        
        console.log(itemFile)
       
        // wx.uploadFile({
        //   url: "https://www.mingchuliangzao.com.cn:8632/common/upload",
        //   filePath: itemFile.path,
        //   name: "file",
        //   formData: {
        //     user: "test"
        //   },
        //   success(res) {
        //     // 上传完成需要更新 fileList
        //     let data = JSON.parse(res.data);
        //     let obj = {
        //       pic: data.url,
        //     };
        //     let picStateInfoList = that.data.picStateInfoList;
        //     picStateInfoList.unshift(obj);
        //     that.setData({
        //       picStateInfoList,
        //     });
        //   },
        //   fail(error) {
        //     wx.showToast({
        //       title: "上传失败，请稍后再试！",
        //     });
        //   },
        // });
      });
    },
  showOrderFlow(e) {
    this.data.params.wid = e.currentTarget.dataset.item.wid
    this.setData({
      showOrderFlow: true
    })
  },
  // 得得得得得的额
  overPeople() {
    let params = {
      // code: "group"
      zhanId:this.data.undoneInfoList.zid
      
    }
    lgRequest("userList1",params, {
      doSuccess: r => {
        console.log(r)
        let overPeople = r.data
        overPeople.forEach(item => {
          item.value = item.userId;
          item.text = item.roleName;
        })
        this.setData({
          overPeople

        })
      }
    })
  },
  // 展示流转人
  showPeople() {
    this.setData({
      showPeople: true
    })
  },
  // 关闭流转人窗口
  onCloseC() {
    this.setData({
      showPeople: false
    })
  },
  // 选择流转人
  selectPeople(e) {
    console.log(e)
    let selectPeople = e.currentTarget.dataset.item.nickName;
    this.data.params.conductor = e.currentTarget.dataset.item.userId;
    this.setData({
      selectPeople
    })
    this.onCloseC();
  },
  // 选择问题
  selectType(e) {
    console.log(e)
    let isType = e.currentTarget.dataset.index;
    let typeCode = e.currentTarget.dataset.typeCode;

    this.setData({
      isType
    })

  },
  // 获取工单流转问题标签
  getProblemTag() {
    let params = {
      code: "problem_type",
    }
    lgRequest("dictionaries", params, {
      doSuccess: r => {
        console.log(r);
        this.setData({
          problemTag: r.data,
        })
      }
    })
  },
  // 选择工单流转问题
  selectproblemTag(e) {

    console.log(e)
    let item = e.currentTarget.dataset.item
    let index = e.currentTarget.dataset.index
    let problemTag = this.data.problemTag;

    this.data.problemType.push(item.valueId)
    console.log(this.data.problemType)
    problemTag[index].is = !problemTag[index].is
    this.data.params.problemType = this.data.problemType.toString();

    this.setData({
      problemTag
    })
  },
  // 获取流转说明
  getOrderFlow(e) {
    console.log(e.detail.value)
    this.data.params.mvremark = e.detail.value
  },
  // 提交工单流转信息
  submitOrderFlow() {
    this.data.params.dqGps = this.data.workList.gps;
    let params = this.data.params;
    lgRequest("submitOrderFlow", params, {
      doSuccess: r => {
        console.log(r)
        this.getOrderList(); //获取工单列表
        this.onClose();
      }
    })

  },
  showOrderCompletion(e) {
    this.data.params.wid = e.currentTarget.dataset.item.wid
    this.setData({
      showOrderCompletion: true
    })
  },
  // 完成工单确定
  OrderCompletion() {
    this.data.params.zid = this.data.undoneInfoList.zid;
    this.data.params.dqGps = this.data.undoneInfoList.gps;
    this.data.params.conductor = this.data.undoneInfoList.conductor;
    this.data.params.state = 2;
    let params = this.data.params
    lgRequest("smbitQuestionPic", params, {
      doSuccess: r => {
        console.log(r)
        this.getOrderList(); //获取工单列表
        this.onClose();
      }
    })
  },
  // 转到历史工单

  toHistory() {
    // debugger
    let info = encodeURIComponent(JSON.stringify(this.data.undoneInfoList));
    wx.navigateTo({
      url: `../historyWorkList/historyWorkList?info=${info}`,
      fail: err => {
        console.log(err)
      }
    })
  },
  // 转到流转记录
  totransfer(e) {
    // let info = e.currentTarget.dataset.item
    let info = encodeURIComponent(JSON.stringify(e.currentTarget.dataset.item));
    wx.navigateTo({
      url: `../transferRecord/transferRecord?info=${info}`,
      fail: err => {
        console.log(err)
      }
    })
  },
  showPic(e) {
    this.setData({
      showPic: true,
      itemPic: e.currentTarget.dataset.item,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options,'options')
    this.setData({
      undoneInfoList: JSON.parse(decodeURIComponent(options.info)),
      // undoneInfoListOne: JSON.parse(options.infos)
    })
    // console.log(this.data.undoneInfoListOne);
    this.getWorkList(); //获取工单详情
    this.getOrderList(); //获取工单列表
    this.noProcessing(); //获取暂不处理原因
    this.overPeople();
    this.getProblemTag(); //获取标签列表
    // this.getProblem();//获取标签列表
    this.getZhanInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})